﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using jSLApp.Client.Controls;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.Authorization;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;
using jSLCore.Client.Presentation.BaseControl.Selection;
using jSLCore.Client.Presentation.jBinding;
using jSLCore.Client.Presentation.Util;

namespace jSLApp.Client.Forms
{
    public partial class CommentList : SelectControl
    {
        public CommentList()
        {
            InitializeComponent();
        }

        public bool IsRatingVisible
        {
            get { return commentGrid.IsRatingVisible; }
            set { commentGrid.IsRatingVisible = value; }
        }

        public override ISelectableGrid GetSelectableGrid()
        {
            return commentGrid;
        }

        public override bool IsStandartOkCancelEnabledWhenNotSelectable
        {
            get { return false; }
        }

        public override void LoadInitialData()
        {
            Refresh();
        }

        public void Refresh()
        {
            if(ParentApplicationContainer==null)return;

            if (CommetableId != null)
                if (jSecurity.IsAuthorized)
            DoWork("Загрузка комментариев", (sender, args) =>
            {
                args.Result = Communication.Proxy.GetComments(args.Int);
            }, (o, eventArgs) =>
            {
                if (eventArgs.Error == null)
                {
                    commentGrid.ItemsSource = (IEnumerable)eventArgs.Result;
                    Bind();
                }
            }, CommetableId);
          
        }

        public event EventHandler DataUpdated;

        public void InvokeDataUpdated()
        {
            EventHandler handler = DataUpdated;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        private int? _commetableId;
        public int? CommetableId
        {
            get { return _commetableId; }
            set
            {
                _commetableId = value;
               Refresh();
            }
        }

        private void createComment_Clicked(object sender, RoutedEventArgs e)
        {
            if (_commetableId != null)
            {
                var dlg = new NewComment((int)_commetableId) { IsRatingEnabled = IsRatingVisible };
                dlg.ShowDialog(delegate(object o, object data)
                                   {
                                       InvokeDataUpdated();
                                       if(!IsRatingVisible)Refresh();
                                   }, this);
            }
        }

        private void reply_Clicked(object sender, QuoteEventArgs e)
        {
            if (_commetableId != null)
            {
                var dlg = new NewComment((int)_commetableId) { IsRatingEnabled = IsRatingVisible };
                dlg.Quote = e.QuoteText;
                dlg.WhoAndWhenQuote = e.WhoAndWhenQuote;
                dlg.ShowDialog(delegate(object o, object data)
                                   {
                                       InvokeDataUpdated();
                                       Refresh();
                                   }, this);
            }
        }

        private void delete_Clicked(object sender, GenericEventArgs<object> genericEventArgs)
        {
            var dlg = new QuestionBox("Вы действительно хотите удалить комментарий? В дальнейшем его будет невозможно восстановить, но вы сможите видеть его текст. Продолжить?",QuestionBoxType.OkCancel);
            dlg.ShowDialog(delegate(object o, object data)
                               {
                                   var comment = (Comment)genericEventArgs.Parameter;

                                   DoWork("Удаление комментария", (sender1, args) =>
                                                                      {
                                                                          Communication.Proxy.DeleteComment(args.Int);
                                                                      }, (o1, eventArgs) =>
                                                                                              {
                                                                                                  if(eventArgs.Error==null)
                                                                                                  {   
                                                                                                      comment.IsDeleted
                                                                                                          = true;
                                                                                                  }
                                                                                              }, comment.id);
                                   
                               },this);
        }
    }
}
